package app.com.itcast.biyesheji.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import app.com.itcast.biyesheji.Adapter.CarAdapter;
import app.com.itcast.biyesheji.Bean.CarBean;
import app.com.itcast.biyesheji.R;

public class CarActivity extends AppCompatActivity {

    private ListView listView;
    private ImageView shouye;
    private ImageView wode;
    private int id;
    private int userid;
    private int carid;
    private Button js;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        initView();
//        initData2();
        initData();
        initEvent();
    }


    private void initView() {
        listView = findViewById(R.id.car_list);
        wode = findViewById(R.id.bottom_bar_image_myinfo);
        shouye = findViewById(R.id.bottom_bar_image_course);
        js = findViewById(R.id.btn_js);
    }

    private void initData() {
        SharedPreferences userinfo = getSharedPreferences("data",MODE_PRIVATE);
        userid = userinfo.getInt("userid",0);
        getData();
    }

    private void getData() {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = getResources().getString(R.string.api_ip) + "/car/momo/list";
        JSONObject jsonObject = new JSONObject();

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e("TAG", "onResponse: " + jsonObject);
                        /*处理结果数据*/
                        // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）
                        Gson gson = new Gson();
                        CarBean myBean = gson.fromJson(jsonObject.toString(), CarBean.class);
                        if (myBean.isSuccess()) {
                            // 获取链表数据购物车userid判定
                            List list = new ArrayList();
                            SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE);
                            int userid = sp.getInt("userid",0);
                            for (int i = 0; i < myBean.getList().size(); i++) {
                                if(userid == myBean.getList().get(i).getUserid()){
                                    list.add(myBean.getList().get(i));
                                }
                            }
                            // 构建适配器对象
                            CarAdapter adapter = new CarAdapter(list, getApplicationContext());
                            adapter.notifyDataSetChanged();
                            // 为listview设置适配器对象
                            listView.setAdapter(adapter);
                        } else {
                            Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    private void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        shouye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CarActivity.this,ShouyeActivity.class);
                startActivity(intent);
            }
        });
        wode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CarActivity.this,WoderightActivity.class);
                startActivity(intent);
            }
        });
        js.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CarActivity.this,ZhifuActivity.class);
                startActivity(intent);
            }
        });
    }

}